package mundo;

import javax.microedition.lcdui.*;
import interfaz.*;


/**
 * This class drives the games (hence the name engine ). It has all the level 
 * data, animates the objects, keeps track of state, and triggers
 * screen redraws.
 */
public class Engine implements Runnable {
    
    // engine states
    public static final int WELCOME = 0;
    public static final int PLAY = 1;
    public static final int SCRIPT = 2;
    public static final int MAP = 3;
    
    public static final int LAST_KEY_DELTA = 7000;

    public RoboMindCanvas screen;
    public int state;
    public int key;
    public long lastKeyPress;
    public boolean done;
    public boolean paused;

     /**
     * Creates Engine object
     *
     * @param screen the screen used for rendering
     */
    public Engine(RoboMindCanvas s) {
    	
    	this.screen = s;
        state = WELCOME;
        screen.setEngine(this);
        done = false;
        lastKeyPress = System.currentTimeMillis();
        Thread runner = new Thread(this);
        runner.start();
    }

     /**
     * Called when a key is pressed
     *
     * @param keycode key code of the key that was pressed
     * @param gameAction game action associated with the given key code of the device
     */
    public void keyPressed(int keycode, int gameAction) {
        key = gameAction;
        lastKeyPress = System.currentTimeMillis();
    }

    /**
     * Called when a key is released
     *
     * @param keycode key code of the key that was pressed
     * @param gameAction game action associated with the given key code of the device
     */    
    public void keyReleased(int keycode, int gameAction) {
        key = 0;
    }

    /**
     * Retrives state of the game
     *
     * @param engineState holder of the state
     */
    public int getState() {
        return state;
    }

    /**
     * Starts the game
     *
     * @param level a level to be started
     */
    public void startGame() {
        state = PLAY;
    }

    /**
     * Sets time of a last pressed key
     *
     * @param time time when the key was pressed
     */
    public void setLastKeyPressed(long time) {
        lastKeyPress = time;
    }

    /**
     * Stops the game
     */
    public void stop() {
        done = true;
    }

    /**
     * Sets a flag indicating whether the game was paused
     *
     *@param pause true if the game is paused, false otherwise
     */
    public void setPaused(boolean pause) {
        paused = pause;
    }
    
    /**
     * Executes the game
     */
    public void run() {
        long then;
        then = System.currentTimeMillis();

        while (!done) {
            if ((System.currentTimeMillis() - lastKeyPress) > LAST_KEY_DELTA) 
            {
                lastKeyPress = System.currentTimeMillis();
            }

            screen.repaint();
            then = System.currentTimeMillis();
        }
    }

     /**
     * Sets engine state
     *
     * @param state a code of the engine state
     */
    public void setState(int s) {
        state = s;
    }
}
